package com.ems.dao;

import com.ems.model.Department;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class DepartmentDAO {
    // 添加部门
    public void addDepartment(Department department) throws SQLException {
        String sql = "INSERT INTO departments (name, description, parent_id) VALUES (?, ?, ?)";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setString(1, department.getName());
            stmt.setString(2, department.getDescription());
            stmt.setInt(3, department.getParentId());
            stmt.executeUpdate();
        }
    }

    // 删除部门
    public void deleteDepartment(int id) throws SQLException {
        String sql = "DELETE FROM departments WHERE id=?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setInt(1, id);
            stmt.executeUpdate();
        }
    }

    // 更新部门信息
    public void updateDepartment(Department department) throws SQLException {
        String sql = "UPDATE departments SET name=?, description=?, parent_id=? WHERE id=?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setString(1, department.getName());
            stmt.setString(2, department.getDescription());
            stmt.setInt(3, department.getParentId());
            stmt.setInt(4, department.getId());
            stmt.executeUpdate();
        }
    }

    // 获取所有部门
    public List<Department> getAllDepartments() throws SQLException {
        List<Department> departments = new ArrayList<>();
        String sql = "SELECT * FROM departments";
        try (Connection conn = DBUtil.getConnection();
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery(sql)) {
            while (rs.next()) {
                Department dept = new Department();
                dept.setId(rs.getInt("id"));
                dept.setName(rs.getString("name"));
                dept.setDescription(rs.getString("description"));
                dept.setParentId(rs.getInt("parent_id"));
                departments.add(dept);
            }
        }
        return departments;
    }

    // 根据ID获取部门
    public Department getDepartmentById(int id) throws SQLException {
        String sql = "SELECT * FROM departments WHERE id=?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setInt(1, id);
            try (ResultSet rs = stmt.executeQuery()) {
                if (rs.next()) {
                    Department dept = new Department();
                    dept.setId(rs.getInt("id"));
                    dept.setName(rs.getString("name"));
                    dept.setDescription(rs.getString("description"));
                    dept.setParentId(rs.getInt("parent_id"));
                    return dept;
                }
            }
        }
        return null;
    }

    // 获取子部门
    public List<Department> getSubDepartments(int parentId) throws SQLException {
        List<Department> departments = new ArrayList<>();
        String sql = "SELECT * FROM departments WHERE parent_id=?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setInt(1, parentId);
            try (ResultSet rs = stmt.executeQuery()) {
                while (rs.next()) {
                    Department dept = new Department();
                    dept.setId(rs.getInt("id"));
                    dept.setName(rs.getString("name"));
                    dept.setDescription(rs.getString("description"));
                    dept.setParentId(rs.getInt("parent_id"));
                    departments.add(dept);
                }
            }
        }
        return departments;
    }
}